<?php

namespace App\Http\Controllers\Api;

use App\Models\AppVersion;
use App\Models\DownloadLog;
use App\Services\FFMpegUtil;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\Validator;

class UploadController extends Controller
{
    protected $img_maxsize =  5242880;// 5M
    protected $video_maxsize = 20*1048576; //20M


    //上传图片
    public function uploadFile(Request $request)
    {
        $video_screenshot = false;

        if($request -> isMethod('post')){

            $file_name = $request -> input('file_name') ?: 'other';

            #  上传文件
            if( $request -> hasFile('file') && $request -> file('file') -> isValid()){

                $profile = $request -> file('file');
//                $path    = Storage::disk('api') -> url('');
                $path    = config('app.img_url');

                $size    = $profile -> getSize();


                // 判断文件是视频文件，还是图片
                $ext = strtolower($profile -> getClientOriginalExtension());
                $img_exts = ['jpg','jpeg','png','gif'];
                if(in_array($ext,$img_exts)){
                    // 判断大小
                    if($size >= $this -> img_maxsize){
                        return jsonError(4041);
                    }
                }else{
                    $mime = $profile -> getMimeType();
                    if ($mime == "video/x-flv" || $mime == "video/mp4" || $mime == "application/x-mpegURL" || $mime == "video/MP2T" || $mime == "video/3gpp" || $mime == "video/quicktime" || $mime == "video/x-msvideo" || $mime == "video/x-ms-wmv")
                    {
                        //判断大小
                        if($size >= $this -> video_maxsize){
                            return jsonError(4040);
                        }

                        $video_screenshot = true;

                    }else{
                        return jsonError(4042);
                    }

                }


                //  上传文件
//                $filename = $profile -> store($file_name.'/'.date('Ymd'),'api');
//                $result = ['file' => 'api/'.$filename,'file_url' => $path.$filename];

                // 文件上传阿里云OSS
                $disk = Storage::disk('oss');
                $filename = $disk->put('api/' . $file_name .'/'. date("Ymd"), $profile);
                $result = ['file' => $filename,'file_url' => $path.$filename];

                // 视频
                if($video_screenshot){
                    // 文件本地上传
//                    $tmp = $profile -> getRealPath();
//                    $video_cover_name = 'api/'.str_before($filename,'.').'.jpg';
//
//                    $videoCover = get_video_cover($tmp, $video_cover_name);
//                    $result = array_merge($result,['video_cover' => $videoCover,'video_cover_url' => config('app.img_url').$videoCover]);

                    // 阿里云OSS获得视频截图
                    $oss_snapshot = '?x-oss-process=video/snapshot,t_0,f_jpg,w_0,h_0,m_fast';
                    $result = array_merge($result,['video_cover' => $filename.$oss_snapshot, 'video_cover_url' => $path.$filename.$oss_snapshot]);
                }

                return jsonSuccess($result);
            }else{
                return jsonError(4026);
            }
        }
    }


    // 多图片上传
    public function uploadImgs(Request $request)
    {
        if($request -> isMethod('post')){

            $file_name = $request -> input('file_name') ?: 'other';

            if($files = $request -> allFiles()){
                if(is_array($files)){

//                    $path = Storage::disk('api') -> url('');
                    $path    = config('app.img_url');
                    $name = [];
                    foreach($files as $file){
                        // 文件本地上传
//                        $filename = $file -> store($file_name.'/'.date('Ymd'),'api');
//                        $img[] = 'api/'.$filename;
//                        $name[] = $path.$filename;

                        // 文件上传阿里云OSS
                        $disk = Storage::disk('oss');
                        $filename = $disk->put('api/' . $file_name . '/' . date("Ymd"), $file);

                        $img[] = $filename;
                        $name[] = $path.$filename;
                    }

                    return jsonSuccess(['imgs' => $img,'imgs_url' => $name]);
                }
            }else{
                return jsonError(4011);
            }
        }
    }















}
